package com.intelligence.device.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligence.common.system.AuthContextUtil;
import com.intelligence.device.manager.service.MonpointsService;
import com.intelligence.entity.pojo.Device;
import com.intelligence.device.manager.service.DeviceService;
import com.intelligence.device.manager.mapper.DeviceMapper;
import com.intelligence.entity.pojo.Farm;
import com.intelligence.entity.pojo.Monpoints;
import com.intelligence.entity.pojo.SysUser;
import com.intelligence.entity.vo.MonpointsVo;
import com.intelligence.openfeign.service.farm.FarmFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【t_device(设备表)】的数据库操作Service实现
 * @createDate 2024-05-27 08:48:37
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device>
        implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private MonpointsService monpointsService;
    @Autowired
    private FarmFeignService farmFeignService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Device> listByMid(Integer mid) {
        List<Device> deviceList = deviceMapper.listByMid(mid);
        return deviceList;
    }

    /**
     * 1、用户登录时获取用户的基地信息存储到数redis中
     * 2、根据用户id存储设备在线状态到redis中
     * 3、取出redis中的设备在线状态，如果没有那么从数据库中获取
     * 获取设备在线状态
     * 1、从redis里面获取（如果为空则从数据库查询）
     * 2、获取设备状态
     **/
    @Override
    public List<MonpointsVo> onlineDevice(Integer farmId) {
        //首先获取用户登录信息
        SysUser sysUser = AuthContextUtil.get();
        if (ObjectUtil.isEmpty(sysUser)) {
            return null;
        }
        //首先去redis中拿取是否已经存在该用户下面的所有设备
        List<MonpointsVo> monpointsVoList = redisTemplate.opsForList().range("device_display_" + sysUser.getId(), 0, -1);
        System.out.println(monpointsVoList);
        if (ObjectUtil.isEmpty(monpointsVoList)) {
            //根据用户登录信息的主键值去redis中拿取基地信息
            List<Farm> farmIdFromUserId = this.farmFeignService.getFarmIdFromUserId(sysUser.getId());
            //先根据基地信息（基地可能有多个）查询出所有的气象站
            List<Monpoints> monpointsList = this.monpointsService.findAllMOnpointsByfarmId(farmIdFromUserId);
            //新建一个数组用来存储我们页面所需要的对象
            monpointsVoList = new ArrayList<>(monpointsList.size());
            BeanUtil.copyProperties(monpointsList, monpointsVoList);
            for (MonpointsVo m : monpointsVoList) {
                System.out.println("我们的设备模块中，是否已经完成了集合拷贝工作？nextTheMessage");
                System.out.println(m.getId());
                List<Device> deviceList = deviceMapper.listByMid(m.getId());
                m.setDeviceList(deviceList);
                redisTemplate.opsForList().leftPush("device_display_" + sysUser.getId(),m);
            }

        }
        return monpointsVoList;
    }
}




